<?php

class PageBuilder {

	public static function getContent() {
		switch( Utils::getPage() ) {
			case 'out':
				self::buildOutStorageHTML();
				break;
			case 'recent':
				self::buildRecentDevicesHTML();
				break;
			case 'users':
				self::buildUsersHTML();
				break;
			case 'contracts':
				self::buildContractsHTML();
				break;
			default:
				self::buildInStorageHTML();
		}
	}
	
	public static function preContent( $devices = false ) {
		echo '<div style="width: 100%;">';
		if ( $devices ) {
			Utils::printDeviceAlphabet( $devices );
		}
		Utils::getExtraPageLinks();
		echo '</div>';
	}
	
	public static function buildContractsHTML() {
		$rentManager = new RentManager();
		echo '<table cellspacing="10" style="width: 100%">';
		echo '<thead><tr><th>Lepingu nr.</th><th>Rentija</th><th>Sõlmitud</th></tr></thead>';
		echo '<tbody>';
		foreach( $rentManager->getAllContracts() as $contract ) {
			$user = $contract->getUser();
			
			echo '<tr>';
			echo '<td><a href="contract-pdf.php?id='.$contract->getID().'" target="_blank" title="Vaata">'.$contract->getStringID().'</a></td>';
			echo '<td>'.$user->getFullname().'</td>';
			echo '<td>'.$contract->getDate().'</td>';
			echo '</tr>';
		}
		echo '</tbody>';
		echo '</table>';
	}
	
	public static function buildRecentDevicesHTML() {
		$deviceManager = new DeviceManager();
		$devices = $deviceManager->getRecentDevices();
		
		self::preContent( $devices );

		$last_char = '';
		foreach( $devices as $device ) {
			$last_char = self::jumpLink( $device, $last_char );
			$class = 'device-row';
			if ( !$device->isInStorage() ) {
				$class = 'device-row-rented';
			}
			echo '<div class="'.$class.' device-row-shadow" id="device-'.$device->getSQL_ID().'">';
			echo '<img src="http://www.hk.tlu.ee/~kuli/hk/qr.php?sisu='.$device->getQRContent().'" id="'.$device->getSQL_ID().'" style="width:100px; height: 100px; z-index: 4" />';
			
			/* Actual device view - default */
			self::buildDeviceMetadata( $device );
			
			echo '</div>';
		}
		
		
		if ( !isSet( $device ) ) {
			echo '<p>&Uuml;htegi seadet ei ole</p>';
		}
	}
	
	public static function buildOutStorageHTML() {
		$deviceManager = new DeviceManager();
		$devices = $deviceManager->getDevicesOutStorage();	
		
		self::preContent( $devices );
		
		$last_char = '';
		foreach( $devices as $device ) {
			$last_char = self::jumpLink( $device, $last_char );
			echo '<div class="device-row-rented device-row-shadow" id="device-'.$device->getSQL_ID().'">';
			echo '<img src="http://www.hk.tlu.ee/~kuli/hk/qr.php?sisu='.$device->getQRContent().'" id="'.$device->getSQL_ID().'" style="width:100px; height: 100px; z-index: 4" />';
			
			/* Actual device view - default */
			self::buildDeviceMetadata( $device );
			
			echo '</div>';
		}
		
		
		if ( !isSet( $device ) ) {
			echo '<p>&Uuml;htegi seadet ei ole</p>';
		}
	}

	public static function buildInStorageHTML() {
		$deviceManager = new DeviceManager();
		$devices = $deviceManager->getDevicesInStorage();	
		
		self::preContent( $devices );
		
		$last_char = '';
		foreach( $devices as $device ) {
			$last_char = self::jumpLink( $device, $last_char );
			echo '<div class="device-row device-row-shadow" id="device-'.$device->getSQL_ID().'">';
			echo '<img src="http://www.hk.tlu.ee/~kuli/hk/qr.php?sisu='.$device->getQRContent().'" id="'.$device->getSQL_ID().'" class="qr-id" style="width:100px; height: 100px; z-index: 4" />';
			
			/* Action buttons */
			self::buildDeviceActionButtons( $device ); 
			/* Edit view */
			self::buildDeviceEditView( $device );
			/* Actual device view - default */
			self::buildDeviceMetadata( $device );
			
			echo '</div>';
		}
		
		
		if ( !isSet( $device ) ) {
			echo '<p>&Uuml;htegi seadet ei ole</p>';
		}
	}
	
	public static function jumpLink( $device, $last_char = '' ) {
		$device_char = Utils::getFirstLetter( $device->getModel() );
		if ( $last_char != $device_char ) {
			$last_char = $device_char;
			echo '<a  name="'.$last_char.'"><h2>'.$last_char.'</h2></a>';
		}
		return $last_char;
	}
	
	public static function buildDeviceEditView( $device = false ) {
		/* Edit view */
		if ( !$device ) {
			return;
		}
		echo '<div class="device-data edit" id="edit-'.$device->getSQL_ID().'" style="display:none;">';
		echo '<form action="service.php" method="post" enctype="multipart/form-data">
			<input type="hidden" name="device-id" value="'.$device->getSQL_ID().'" />
			<input type="hidden" name="service_action" value="3" />';
		echo '<input type="text" value="'.$device->getModel().'" name="device_model" required="required" style="width: 280px; font-weight: bold;"/>';	
		echo '<textarea rows="1" placeholder="Seadme / komplekti lühikirjeldus" cols="44" id="desc-'.$device->getSQL_ID().'" class="device_desc_edit" name="device_description">'.$device->getRawDescription().'</textarea>';
		echo '<br /><b>S/N:</b> <input type="text" maxlength="250" name="device_serial" required="required" autocomplete="off" value="'.$device->getSerial().'" placeholder="Seerianumber / IMEI" style="width: 250px" />';
		echo '<br /><b>Väärtus:</b> <input type="text" maxlength="10" name="device_value" value="'.$device->getRawValue().'" autocomplete="off" placeholder="Väärtus €" style="width: 226px"/>';
		echo '<br /><b>Pilt:</b> <input type="file" style="border: 0px" name="device_image_uploaded" />';
		echo '<br /><a href="#" class="cancel-edit" style="margin-left: 147px">Katkesta</a><button style="margin-left: 10px" type="submit" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false">
				<span id="device-btn-text" class="ui-button-text">Muuda</span>
			</button>';
		echo '</form>';
		echo '</div>';
	}
	
	public static function buildDeviceMetadata( $device = false ) {
		if ( !$device ) {
			return;
		}
		echo '<div class="device-data" id="data-'.$device->getSQL_ID().'">';
		echo '<b>'.$device->getModel().'</b>';
		echo '<p>'.$device->getDescription();
		echo '<p><b>S/N:</b> '.$device->getSerial();
		echo '<br /><b>Väärtus:</b> '.$device->getValue().'</i></p>';
		
		switch( Utils::getPage() ) {
			case 'out':
				echo '<p><b>Välja antud:</b> '.$device->getOutDate().'<br/>';
				echo '<b>Tagastamine:</b> '.$device->getReturnDate().'</p>';
				break;
			case 'recent':
				echo '<p><b style="color: red">'.( $device->isInStorage() ? 'KOHAL' : 'VÄLJAS' ).'</b></p>';
				break;
		}
		
		if ( ( $returnedOn = $device->getReturnedDate() ) && $device->isInStorage() ) {
			echo '<p><b>Tagastatud:</b> '.$returnedOn.'</p>';
		}
		
		echo '</div>';
		if ( ( $image = $device->getImage() ) ) {
			echo '<a href="uploads/'.$device->getImage().'" title="'.$device->getModel().'" rel="device" class="device-photo-hook"><img src="uploads/'.$device->getImage().'" class="device_photo" /></a>';
		}
	}
	
	public static function buildDeviceActionButtons( $device = false ) {
		if ( !$device ) {
			return;
		}
		echo '<div id="fader-'.$device->getSQL_ID().'" class="device-data" style="display:none;">';
		echo '<span class="bubble rent-device-drop" " style="margin-left: 75px">Muuda</span>';
		echo '<span class="bubble delete-device-drop" style="float:right; right: 75px;">Kustuta</span>';
		echo '</div>';
	}
	
	public static function buildUsersHTML() {
		$rentManager = new RentManager();
		foreach( $rentManager->getAllUsers() as $user ) {
			echo '<div class="user-row device-row-shadow">';
			echo '<div class="user-data"">';
			echo '<b>'.$user->getFullname().'</b>';
			if ( $user->hasEmail() ) {
				echo '<p><a href="mailto:'.$user->getEmail().'">'.$user->getEmail().'</a>';
			}
			if ( $user->hasPhone() ) {
				echo '<p><b>Tel:</b> <a href="tel:'.$user->getPhone().'">'.$user->getPhone().'</a></p>';
			}
			
			foreach( $rentManager->getRecentUserDevices( $user ) as $device ) {
				echo '<p><b>'.$device->getModel().'</b><br />S/N: '.$device->getSerial().'</p>';
			}
			
			echo '</div>';
			echo '</div>';
		}
	}
}

?>